package com.com.twotestlist;

public class ArrayLIst<E> implements List{
    Object data[];

    public ArrayLIst() {
        this(4);
    }

    public ArrayLIst(int size) {
        data=new Object[size];
    }

    int size;
    @Override
    public int size() {
        return size;
    }

    @Override
    public Object get(int i) {
        if(i<0 || i>=size){
            throw new MyOutIndexException("数组超出");
        }
        return data[i];
    }

    @Override
    public boolean isEmpty() {
        return size==0;
    }

    @Override
    public boolean contains(Object e) {
        return false;
    }

    @Override
    public int indexOf(Object e) {
        return 0;
    }

    @Override
    public void add(int i, Object e) {
        grow();
for(int j=size;j>i;j--){
    data[j]=data[j-1];
}
data[i]=e;
size++;
    }
    public void grow(){
        if(size==data.length){
            Object[] twoArray=new Object[size*2];
            for (int i=0;i<data.length;i++){
                twoArray[i]=data[i];
            }
            data=twoArray;

        }
    }

    @Override
    public void add(Object e) {
     add(size,e);
    }

    @Override
    public boolean addBefore(Object obj, Object e) {
        return false;
    }

    @Override
    public boolean addAfter(Object obj, Object e) {
        return false;
    }

    @Override
    public Object remove(int i) {
        for(int j=size-1;j>i;j--){
            data[j-1]=data[j];
        }
        data[size-1]=null;
        size--;
        return -1;
    }

    @Override
    public boolean remove(Object e) {
        if(e==null){
            for (int i=0;i<size;i++){
                data[i]=null;
                remove(i);
                return true;
            }
        }else{
            for (int i=0;i<size;i++){
                if(e.equals(data[i])){
                    remove(i);
                    return true;
                }

            }
            return false;
        }
        return false;
    }

    @Override
    public Object replace(int i, Object e) {
        return null;
    }

    @Override
    public String toString() {
        StringBuilder str=new StringBuilder();
        str.append("[");
        for(int i=0;i<size;i++){
            if(i!=size-1){
            str.append(data[i]+",");
            }else{
                str.append(data[i]+"]");
            }

        }
        return str.toString();
    }
}
